import threading

class SGSCondition():
    def __init__(self):
        self.cond = threading.Condition()
    
    def wait(self, deskId, cmds):
        ''' wait for cmd from user
            a. match any user if user is None
            b. match any cmd if cmd is None
        '''
        self.deskId = deskId
        self.cmds = cmds
        print '======== Wait cmd %s from %s =======' % (cmds, deskId)
        self.cond.wait()
    
    def notify(self, deskId, cmd):
        deskId = int(deskId)
        if self.deskId != None and self.deskId != deskId:
            return False
        if self.cmds and cmd not in self.cmds:
            return False
        self.cond.notify()
        return True
    
    def acquire(self):
        self.cond.acquire()
        
    def release(self):
        self.cond.release()